<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="房间人数" prop="peopleNumber">
        <el-input v-model="queryParams.peopleNumber" placeholder="请输入房间人数" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品Id" prop="spuId">
        <el-input v-model="queryParams.spuId" placeholder="请输入商品Id" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="规格ID" prop="skuId">
        <el-input v-model="queryParams.skuId" placeholder="请输入规格ID" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="购买数量" prop="buyNumber">
        <el-input v-model="queryParams.buyNumber" placeholder="请输入购买数量" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="胜利者" prop="victoryUserId">
        <el-input v-model="queryParams.victoryUserId" placeholder="请输入胜利者" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['admin:guess:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['admin:guess:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['admin:guess:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['admin:guess:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="guessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="竞猜id" align="center" prop="guessId" /> -->
      <el-table-column label="房间人数" align="center" prop="peopleNumber" />
      <el-table-column label="商品Id" align="center" prop="spuId" />
      <el-table-column label="规格ID" align="center" prop="skuId" />
      <el-table-column label="购买数量" align="center" prop="buyNumber" />
      <el-table-column label="胜利者" align="center" prop="victoryUserId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['admin:guess:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['admin:guess:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改竞猜对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房间人数" prop="peopleNumber">
          <el-input v-model="form.peopleNumber" placeholder="请输入房间人数" />
        </el-form-item>
        <el-form-item label="商品Id" prop="spuId">
          <el-input v-model="form.spuId" placeholder="请输入商品Id" />
        </el-form-item>
        <el-form-item label="规格ID" prop="skuId">
          <el-input v-model="form.skuId" placeholder="请输入规格ID" />
        </el-form-item>
        <el-form-item label="购买数量" prop="buyNumber">
          <el-input v-model="form.buyNumber" placeholder="请输入购买数量" />
        </el-form-item>
        <el-form-item label="胜利者" prop="victoryUserId">
          <el-input v-model="form.victoryUserId" placeholder="请输入胜利者" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGuess, getGuess, delGuess, addGuess, updateGuess, exportGuess } from "@/api/admin/guess";

export default {
  name: "Guess",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 竞猜表格数据
      guessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        peopleNumber: null,

        spuId: null,

        skuId: null,

        buyNumber: null,

        victoryUserId: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询竞猜列表 */
    getList() {
      this.loading = true;
      listGuess(this.queryParams).then(response => {
        this.guessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        guessId: null,

        peopleNumber: null,

        spuId: null,

        skuId: null,

        buyNumber: null,

        victoryUserId: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.guessId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加竞猜";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const guessId = row.guessId || this.ids
      getGuess(guessId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改竞猜";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.guessId != null) {
            updateGuess(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGuess(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guessIds = row.guessId || this.ids;
      this.$confirm('是否确认删除竞猜编号为"' + guessIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delGuess(guessIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有竞猜数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportGuess(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
